package com.aaa.hospital.Client.controller;

import com.aaa.hospital.Client.pojo.Department;
import com.aaa.hospital.Client.service.Impl.DepartmentServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/department")
public class DepartmentCon {
    @Autowired
    private DepartmentServiceImpl departmentServiceImpl;

    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    // 将java对象转为json格式的数据
    @ResponseBody
    //查询学生信息
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<Department> data = departmentServiceImpl.queryAll();
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> insert(Department department) {
        int result = departmentServiceImpl.insert(department);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> update(Department department) {
        int result = departmentServiceImpl.update(department);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //删除
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    //@PathVariable注解绑定它传过来的值到方法的参数上。
    private Map<String, Object> delete(@PathVariable("id") int id) {
        int result = departmentServiceImpl.delete(id);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
}
